﻿using PLCS.App.Shared.Events;
using PLCS.App.Shared.Service.Dialog;
using RegionManager = PLCS.App.Shared.ConstantManagers.RegionManager;

namespace PLCS.App.Shared.Extensions;

public static class DialogExtensions
{
    /// <summary>
    /// 推送等待消息
    /// </summary>
    /// <param name="aggregator"></param>
    /// <param name="model"></param>
    public static void UpdateLoading(this IEventAggregator aggregator, UpdateModel model)
    {
        aggregator.GetEvent<UpdateLoadingEvent>().Publish(model);
    }

    /// <summary>
    /// 注册等待消息
    /// </summary>
    /// <param name="aggregator"></param>
    /// <param name="action"></param>
    public static void Resgiter(this IEventAggregator aggregator, Action<UpdateModel> action)
    {
        aggregator.GetEvent<UpdateLoadingEvent>().Subscribe(action);
    }

    /// <summary>
    /// 弹出消息窗口
    /// </summary>
    /// <param name="dialogService"></param>
    /// <param name="title">标题</param>
    /// <param name="message">提示消息</param>
    public static void ShowMessage(this IDialogService dialogService, string title, string message)
    {
        try
        {
            ShowMsgBoxModel model = new ShowMsgBoxModel()
            {
                Icon = "CommentProcessingOutline",
                Title = title,
                BtnHide = true,
                Msg = message,
            };

            DialogParameters parameters = new DialogParameters
        {
            { "Value", model }
        };

            dialogService.ShowDialog(RegionManager.MessageBox, parameters, callback => { });
        }
        catch { }
       
    }

    /// <summary>
    /// 询问窗口
    /// </summary>
    /// <param name="hostDialogService"></param>
    /// <param name="message">提示消息</param>
    /// <param name="IdentifierName">会话ID</param>
    /// <returns></returns>
    public static async Task<bool> Question(this IHostDialogService hostDialogService,
        string message,
        string IdentifierName = "Root")
    {
        return await hostDialogService.Question(message, IdentifierName);
    }

    /// <summary>
    /// 询问窗口-指定标题
    /// </summary>
    /// <param name="hostDialogService"></param>
    /// <param name="title">标题</param>
    /// <param name="message">提示消息</param>
    /// <param name="IdentifierName">会话ID</param>
    /// <returns></returns>
    public static async Task<bool> Question(this IHostDialogService hostDialogService,
        string title,
        string message,
        string IdentifierName = "Root")
    {
        ShowMsgBoxModel model = new ShowMsgBoxModel()
        {
            Icon = "CommentQuestionOutline",
            Title = title,
            BtnHide = false,
            Msg = message,
        };

        DialogParameters parameters = new DialogParameters
        {
            { "Value", model }
        };

        var dialogResult = await hostDialogService.ShowDialogAsync(RegionManager.MessageBox, parameters, IdentifierName);

        return dialogResult.Result == ButtonResult.OK;
    }

    /// <summary>
    /// 询问窗口
    /// </summary>
    /// <param name="dialogService"></param>
    /// <param name="title">标题</param>
    /// <param name="message">提示消息</param>
    /// <returns></returns>
    public static bool Question(this IDialogService dialogService, string title, string message)
    {
        if (string.IsNullOrWhiteSpace(title))
            title = "确认此操作吗";

        ShowMsgBoxModel model = new ShowMsgBoxModel()
        {
            Icon = "CommentQuestionOutline",
            Title = title,
            BtnHide = false,
            Msg = message,
        };

        DialogParameters parameters = new DialogParameters
        {
            { "Value", model }
        };

        bool dialogResult = false;
        dialogService.ShowDialog(RegionManager.MessageBox, parameters, callback =>
        {
            dialogResult = callback.Result == ButtonResult.Yes;
        });
        return dialogResult;
    }
}